package com.atguigu.lease.web.admin.controller.apartment;

import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.FeeKey;
import com.atguigu.lease.model.entity.FeeValue;
import com.atguigu.lease.web.admin.service.FeeKeyService;
import com.atguigu.lease.web.admin.service.FeeValueService;
import com.atguigu.lease.web.admin.vo.fee.FeeKeyVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "房间杂费管理")
@RestController
@RequestMapping("/admin/fee")
public class FeeInfoController {
    @Autowired
    FeeKeyService feeKeyService;
    @Autowired
    FeeValueService feeValueService;

    @Operation(summary = "查询全部属性名称和属性值列表")
    @GetMapping(value ="list")
    public Result feeInfoList(){
        List<FeeKeyVo> feeKeyVoList = feeKeyService.feeInfoList();
        return Result.ok(feeKeyVoList);

    }





    @Operation(summary = "新增或更新属性名称")
    @PostMapping(value ="key/saveOrUpdate")
    public Result saveOrUpdateFeeKey(@RequestBody FeeKey feeKey){
        feeKeyService.saveOrUpdate(feeKey);
        return Result.ok();
    }

    @Operation(summary = "新增或更新属性值")
    @PostMapping(value ="value/saveOrUpdate")
    public Result saveOrUpdateFeeValue(@RequestBody FeeValue feeValue){
        feeValueService.saveOrUpdate(feeValue);
        return Result.ok();
    }

    @Operation(summary = "根据id删除属性名称")
    @DeleteMapping(value ="key/deleteById")
    public Result deleteFeeKeyById(@RequestParam Long feeKeyId){
        feeKeyService.removeById(feeKeyId);

        LambdaQueryWrapper<FeeValue> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FeeValue::getFeeKeyId,feeKeyId);
        feeValueService.remove(wrapper);

        return Result.ok();
    }

    @Operation(summary = "根据id删除属性值")
    @DeleteMapping(value ="value/deleteById")
    public Result deleteFeeValueById(@RequestParam Long id){
        feeValueService.removeById(id);
        return Result.ok();
    }

}
